package com.example.demo.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import com.example.demo.config.DBContextHolder;

/**
 * @author 作者 zj:
 * @version 创建时间：2019年2月1日 下午12:03:48
 * 路由AOP类
 * 默认 查询走slave   插入修改删除走master
 * 特殊情况下， 需要直接从master读，比如insert 后 获取id
 */
@Component
@Aspect
public class DataSourceAop {

	@Pointcut("!@annotation(com.example.demo.anno.Master)"
				+ "&& (execution(* com.example.demo.service..*.select*(..)) "
				+ "|| execution(* com.example.demo.service..*.find*(..)) "
				+ "|| execution(* com.example.demo.service..*.get*(..)) "
				+ "|| execution(* com.example.demo.service..*.query*(..)))"
			)
	public void readPointCut(){
		
	}
	@Pointcut("@annotation(com.example.demo.anno.Master)"
			+ "&& (execution(* com.example.demo.service..*.insert*(..)) "
			+ "|| execution(* com.example.demo.service..*.add*(..)) "
			+ "|| execution(* com.example.demo.service..*.save*(..)) "
			+ "|| execution(* com.example.demo.service..*.update*(..))"
			+ "|| execution(* com.example.demo.service..*.edit*(..))"
			+ "|| execution(* com.example.demo.service..*.delete*(..))"
			+ "|| execution(* com.example.demo.service..*.remove*(..)))"
		)
	public void writePointCut(){
		
	}
	
	@Before("readPointCut()")
	public void read(){
		DBContextHolder.slave();
	}
	
	@Before("writePointCut()")
	public void write(){
		DBContextHolder.master();
	}
}
